package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanBedEntity;
import city.spring.modules.oldman.service.OldmanBedService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：老人入住床位
 *
 * @author 谢金婷
 * @date 2020-08-18 17:22:40
 */
@RestController
@RequestMapping("/oldman_bed")
public class OldmanBedController extends ApiController {
    private final OldmanBedService oldmanBedService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanBedEntity, ?>> entityOrderFields;

    public OldmanBedController(OldmanBedService oldmanBedService) {
        this.oldmanBedService = oldmanBedService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanBedEntity::getId);
        entityOrderFields.add(OldmanBedEntity::getOldmanId);
        entityOrderFields.add(OldmanBedEntity::getBedId);
        entityOrderFields.add(OldmanBedEntity::getBedType);
        entityOrderFields.add(OldmanBedEntity::getDiscounted);
        entityOrderFields.add(OldmanBedEntity::getDiscountEndtime);
        entityOrderFields.add(OldmanBedEntity::getIsPrivateroom);
        entityOrderFields.add(OldmanBedEntity::getFee);
        entityOrderFields.add(OldmanBedEntity::getBedDescription);
        entityOrderFields.add(OldmanBedEntity::getEffectivetime);
        entityOrderFields.add(OldmanBedEntity::getRemark);
    }

    /**
     * 获取全部的 <strong>老人入住床位</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, OldmanBedEntity entity) {
        LambdaQueryChainWrapper<OldmanBedEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>老人入住床位</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, OldmanBedEntity entity) {
        LambdaQueryChainWrapper<OldmanBedEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<OldmanBedEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>老人入住床位</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanBedService.getById(id));
    }

    /**
     * 添加一个 <strong>老人入住床位</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanBedEntity entity) {
        oldmanBedService.saveOldmanBed(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>老人入住床位</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanBedEntity entity) {
        entity.setId(id);
        oldmanBedService.updateOldmanBed(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>老人入住床位</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanBedService.deleteOldmanBed(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>老人入住床位</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanBedService.deleteOldmanBed(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanBedEntity> buildLambdaQuery(OldmanBedEntity entity) {
        LambdaQueryChainWrapper<OldmanBedEntity> lambdaQuery = oldmanBedService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanBedEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), OldmanBedEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBedId()), OldmanBedEntity::getBedId, entity.getBedId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBedType()), OldmanBedEntity::getBedType, entity.getBedType());
        lambdaQuery.eq(entity.getDiscounted() != null, OldmanBedEntity::getDiscounted, entity.getDiscounted());
        lambdaQuery.eq(entity.getDiscountEndtime() != null, OldmanBedEntity::getDiscountEndtime, entity.getDiscountEndtime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getIsPrivateroom()), OldmanBedEntity::getIsPrivateroom, entity.getIsPrivateroom());
        lambdaQuery.eq(entity.getFee() != null, OldmanBedEntity::getFee, entity.getFee());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBedDescription()), OldmanBedEntity::getBedDescription, entity.getBedDescription());
        lambdaQuery.eq(entity.getEffectivetime() != null, OldmanBedEntity::getEffectivetime, entity.getEffectivetime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRemark()), OldmanBedEntity::getRemark, entity.getRemark());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanBedEntity> buildQuery(OldmanBedEntity entity) {
        QueryWrapper<OldmanBedEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "oldman_id", entity.getOldmanId());
        wrapper.like(StringUtils.isNotBlank(entity.getBedId()), "bed_id", entity.getBedId());
        wrapper.like(StringUtils.isNotBlank(entity.getBedType()), "bed_type", entity.getBedType());
        wrapper.eq(entity.getDiscounted() != null, "discounted", entity.getDiscounted());
        wrapper.eq(entity.getDiscountEndtime() != null, "discount_endtime", entity.getDiscountEndtime());
        wrapper.like(StringUtils.isNotBlank(entity.getIsPrivateroom()), "is_privateroom", entity.getIsPrivateroom());
        wrapper.eq(entity.getFee() != null, "fee", entity.getFee());
        wrapper.like(StringUtils.isNotBlank(entity.getBedDescription()), "bed_description", entity.getBedDescription());
        wrapper.eq(entity.getEffectivetime() != null, "effectivetime", entity.getEffectivetime());
        wrapper.like(StringUtils.isNotBlank(entity.getRemark()), "remark", entity.getRemark());
        return wrapper;
    }

    /**
     * 根据老人id获取床位信息
     *
     * @param oldmanId
     * @return
     */
    @GetMapping("/getBedInfoByoldmanId")
    public Object getBedInfoByoldmanId(String oldmanId) {
        return success(oldmanBedService.getBedInfoByoldmanId(oldmanId));
    }

    /**
     * 根据老人id获取床位信息 List<OldmanBedEntity>
     *
     * @param personId
     * @return
     */
    @GetMapping("/getBedByPersonId/{personId}")
    public Object getBedByPersonId(@PathVariable String personId) {
        return success(oldmanBedService.getBedByPersonId(personId));
    }


    /**
     * 根据老人id得到对应得床位，床位对应的房间类型，房间类型的月价格、优惠，优惠期限
     *
     * @param personId
     * @return
     */
    @GetMapping("/getBedPriceAndDiscountByPersonId/{personId}")
    public Object getBedPriceAndDiscountByPersonId(@PathVariable String personId) {
        return success(oldmanBedService.getBedPriceAndDiscountByPersonId(personId));
    }
}
